package com.example.serviceheadlines.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.serviceheadlines.mapper.HeadlinesCategoryMapper;
import com.example.serviceheadlines.service.HeadlinesCategoryService;
import com.example.serviceheadlines.service.IHeadlinesCategorySortService;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.util.CommStatus;
import com.tangyuan.model.HeadlinesCategory;
import com.tangyuan.model.HeadlinesCategorySort;
import com.tangyuan.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class HeadlinesCategoryServiceImpl extends ServiceImpl<HeadlinesCategoryMapper, HeadlinesCategory> implements HeadlinesCategoryService {

    @Autowired
    private IHeadlinesCategorySortService headlinesCategorySortService;

    @Override
    public Result getHeadlinesCategory(String token) {
        User user = CommStatus.getUser(token);

        List<HeadlinesCategorySort> listed = headlinesCategorySortService.list(new LambdaQueryWrapper<HeadlinesCategorySort>()
                .eq(HeadlinesCategorySort::getUserId, user.getId())
                .eq(HeadlinesCategorySort::getIsDelete, 0));

        if(CollectionUtil.isEmpty(listed)){
            //如果用户没有分类数据  则创建分类数据并添加数据库
            List<HeadlinesCategory> categories = this.list(new LambdaQueryWrapper<HeadlinesCategory>()
                    // .lt(HeadlinesCategory::getSort, 6)
                    .eq(HeadlinesCategory::getIsDelete,0));
            List<HeadlinesCategorySort> sortList = new ArrayList<>();
            categories.forEach(category -> {
                HeadlinesCategorySort sort = new HeadlinesCategorySort();
                sort.setCategoryId(category.getId());
                sort.setUserId(user.getId());
                sort.setCreateTime(new Date());
                sortList.add(sort);
            });
            headlinesCategorySortService.saveBatch(sortList);
        }
        return Result.ok(baseMapper.getHeadlinesCategory(user.getId()));
    }

    @Override
    public void updateCategorySort(String token, HeadlinesCategory headlinesCategory) {
        User user = CommStatus.getUser(token);

        HeadlinesCategorySort categorySort = headlinesCategorySortService.getOne(new LambdaQueryWrapper<HeadlinesCategorySort>()
                .eq(HeadlinesCategorySort::getCategoryId, headlinesCategory.getId())
                .eq(HeadlinesCategorySort::getUserId, user.getId()));
        if (categorySort == null) {
            categorySort = new HeadlinesCategorySort();
            categorySort.setCategoryId(headlinesCategory.getId());
            categorySort.setUserId(user.getId());
            categorySort.setCreateTime(new Date());
            headlinesCategorySortService.save(categorySort);
        } else {
            headlinesCategorySortService.removeById(categorySort.getId());
        }
    }

    @Override
    public Long getHeadlinesCategoryByChannelName(String channelName) {

        HeadlinesCategory headlinesCategory = this.getOne(new LambdaQueryWrapper<HeadlinesCategory>()
                .eq(HeadlinesCategory::getChannelName, channelName)
                .eq(HeadlinesCategory::getIsDelete, 0));

        return headlinesCategory.getId();
    }
}
